#!/usr/bin/python

import xml.etree.ElementTree as ET
import random
import os
import sys

text = ''
textlist = []
nodenum = 0		# total node number
linknum = 0
nodelem = []  # node elements list
node = []		  # node name list
nclass = []  	# virtual node class #			- index as node list
nfi = []		  # node fidelity					    - index as node list
ncpu = []		  # host node desire CPU			- index as node list
nram = []   	# host node desire RAM			- index as node list
nhdd = []   	# host node desire HDD			- index as node list
nint = []
linkelem = []
link = []	    # link name list
lnode1 = []   # one end node of link			  - index as link list
lnode2 = []   # the other end node of link	- index as link list
lbw = []	    # link desire bandwidth			  - index as link list
ldelay = []   # link desire delay				    - index as link list

try:
	file_object = open(sys.argv[1])
	all_the_text = file_object.read()
except:
	sys.exit("Please specify the ns file as input!\r\n")
else:
	file_object.close()

# Read each line into textlist:
for x in all_the_text:
	if x != '\n':
		text = text + x
	else:
		text = text + '\n'
		textlist.append(text)
		text = ''

# Initialize the node related info
for y in textlist:
  if y.find('[$ns node]') > -1:
    node.append(y[y.index(' ')+1:y.index('[$ns node]')-1])
    nclass.append(random.randint(1,10))	# Initialize vnode class
    nfi.append(random.randint(1,10))	# Generate node fidelity
    ncpu.append(random.randint(1,5))	# Generate host cpu demand
    nram.append(random.randint(1,5))	# Generate host ram demand
    nhdd.append(random.randint(1,10)*10)# Generate host hdd demand
    nodenum = nodenum + 1
    
# Initialize the link related info
for y in textlist:
  if y[0:8] == 'set Link':
    link.append(y[y.index('$')+1:len(y)])  # Record link summary
    k1 = link[-1].index('$')
    k2 = link[-1].index(' ', k1)
    lnode1.append(link[-1][k1+1:k2])	# Record one end node
    k3 = link[-1].index(' ', k2+1)
    lnode2.append(link[-1][k2+2:k3])	# Record another end node
    #k4 = link[-1].index('Mb', k3)
    lbw.append(100*(random.randint(1,1)))	# Generate link bandwidth
    #k5 = link[-1].index('ms', k4)
    ldelay.append(random.randint(2,4))	# Generate link delay
    linknum = linknum + 1

root = ET.Element('Node_Link_Info')

nodeinfo = ET.Element('VNodes')
root.append(nodeinfo)
for i in range(nodenum):
  nodelem.append(ET.Element(node[i]))
  nodeinfo.append(nodelem[i])
  nodelem[i].attrib['VClassID'] = str(nclass[i])
  nodelem[i].attrib['FidelityIndex'] = str(nfi[i])
  nodelem[i].attrib['CPUReq_GHz'] = str(ncpu[i])
  nodelem[i].attrib['RAMReq_GB'] = str(nram[i])
  nodelem[i].attrib['HDDReq_GB'] = str(nhdd[i])

linkinfo = ET.Element("VLinks")
root.append(linkinfo)
for i in range(linknum):
  linkelem.append(ET.Element('Link' + str(i)))
  linkinfo.append(linkelem[i])
  linkelem[i].attrib['EndNode1'] = str(lnode1[i])
  linkelem[i].attrib['EndNode2'] = str(lnode2[i])
  linkelem[i].attrib['BandWidth_Mb'] = str(lbw[i])
  linkelem[i].attrib['Delay_ms'] = str(ldelay[i])

file_object = open(sys.argv[1][0:sys.argv[1].find('.ns')]+'_ninfo.xml','w')
ET.ElementTree(root).write(file_object)
file_object.close()
